package cn.skylark.wcm.controller;

import cn.skylark.wcm.common.util.ReturnMap;
import cn.skylark.wcm.entity.Constant;
import cn.skylark.wcm.entity.Tenan;
import cn.skylark.wcm.entity.Tenanproperty;
import cn.skylark.wcm.service.TenanPropertyService;
import cn.skylark.wcm.service.TenanService;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 租户属性控制器类
 *
 * @author 武文旭
 * @Date 20180109
 * @since
 */
@RestController
@RequestMapping(value = "/tenanproperty")
public class TenanPropertyController {
    @Autowired
    TenanPropertyService tenanPropertyService;
    @Autowired
    TenanService tenanService;
    /**
     * logger
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(TenanPropertyController.class);

    /**
     * 根据租户id获取租户属性
     * 业务:通过tenanid查询租户属性表，并返回租户属性数据
     *
     * @param tenanproperty
     * @return Map
     * @Exception e 数据库错误异常
     */
    @RequestMapping("/gettenanProperty")
    public String gettenanProperty(Tenanproperty tenanproperty) {
        LOGGER.info("根据租户id获取租户属性开始 租户id:" + tenanproperty.getTenanid());
        ReturnMap returnMap = new ReturnMap();
        Map<String, Object> map = null;
        try {
            map = tenanPropertyService.getPropertyByTenanid(tenanproperty);
            returnMap.setData(JSONObject.fromObject(map).toString());
            returnMap.setMessage("租户属性取得！");
            returnMap.setFlg(0);
            LOGGER.info("根据租户id获取租户属性结束");
        } catch (Exception e) {
            LOGGER.info("异常信息为=" + e);
            e.printStackTrace();
            returnMap.setMessage("数据库异常！");
            returnMap.setFlg(1);
        }
        return returnMap.ReturnMap();
    }

    /**
     * 保存租户属性信息
     * 1、我理解每次传入一个Tenanproperty对象进行保存
     * 2、如果保存的信息之前没有就新增，如果有就更改
     * 3、如果返回1 说明有数据，进行变更 如果返回0说明没数据进行新增
     *
     * @param property 租户属性信息
     * @return 返回message提示信息
     * @Exception e 数据库连接异常
     */
    @RequestMapping("/saveTenanProperty")
    public String saveTenanProperty(String property, Integer tenanid) {
        LOGGER.info("保存租户属性信息开始 租户属性=" + property + "租户id=" + tenanid);
        ReturnMap ret = new ReturnMap();
        try {
            String propertyMessage = property.substring(1, property.length() - 1).replaceAll("\"", "");
            String[] propertyId = propertyMessage.split(",");
            Tenanproperty tenanproperty;
            Tenanproperty tenanpropertyMessage;
            Tenanproperty tenanpropertyMessageisnull;
            Constant constantMessage;
            for (int i = 0; i < propertyId.length; i++) {
                LOGGER.info("====" + propertyId[i]);
                tenanproperty = new Tenanproperty();
                String strMessage = propertyId[i].toString();
                int str = strMessage.indexOf(":");
                String keyname = strMessage.substring(0, str);
                String keyvalue = strMessage.substring(str + 1, strMessage.length()).trim();
                Constant constant = new Constant();
                constant.setKeyname(keyname);
                constantMessage = tenanPropertyService.getConstantByKeyName(constant);
                tenanproperty.setTenanid(tenanid);
                tenanproperty.setKeycode(constantMessage.getKeycode());
                Integer count = tenanPropertyService.selectTenanPropertyByKeycode(tenanproperty);
                if (count > 0) {
                    tenanpropertyMessage = tenanPropertyService.selectTenanPropertyOne(tenanproperty);
                    tenanPropertyService.addTenanProperty(tenanpropertyMessage, keyvalue);
                } else {
                    Tenan tenan = new Tenan();
                    tenan.setId(tenanid);
                    Tenan tenanMessage = tenanService.getTenanByOne(tenan);
                    tenanpropertyMessageisnull = new Tenanproperty();
                    tenanpropertyMessageisnull.setTenancode(tenanMessage.getTenancode());
                    tenanpropertyMessageisnull.setTenanid(tenanid);
                    tenanpropertyMessageisnull.setDelflg(0);
                    tenanpropertyMessageisnull.setKeycode(constantMessage.getKeycode());
                    tenanPropertyService.addTenanProperty(tenanpropertyMessageisnull, keyvalue);
                }
            }
            LOGGER.info("保存租户属性信息结束");
            ret.setFlg(0);
            ret.setMessage("保存租户属性信息成功");
            return ret.ReturnMap();
        } catch (Exception e) {
            LOGGER.info("异常信息为=" + e);
            e.printStackTrace();
            ret.setFlg(2);
            ret.setMessage("保存租户属性信息失败！");
            return ret.ReturnMap();
        }
    }
}
